﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using G15_Agent.Models.ViewModels;
using G15_Agent.Models.Travel;
using G15_Agent.BUS;
using G15_Agent.Helpers.ServiceHelpers;
using G15_Agent.Helpers.ServiceHelpers.TravelServiceHelpers;
using G15_Agent.Models.ViewModels.TravelViewModels;
using G15_Agent.Helpers;

namespace G15_Agent.Controllers
{
    public class TravelController : Controller
    {
        private static BUS_Travel serviceContext = BUS_Manager.travel;

        public ActionResult Index()
        {
            List<string> listService = new List<string>();
            foreach (var service in ServiceFactory.Travel)
            {
                listService.Add(service.ServiceID);
            }

            return View(listService);
        }

        public ActionResult SearchTravel(string serviceID)
        {
            bool isError = false;
            var service = (TravelSerivce)ServiceFactory.GetService(serviceID);
            SearchTravelViewModel viewModel = new SearchTravelViewModel();

            try
            {
                viewModel.serviceID = serviceID;
                viewModel.ngayDi = DateTime.Today;
                viewModel.ngayVe = DateTime.Today;
            }
            catch (Exception ex)
            {
                isError = true;
            }

            if (isError || serviceID == null)
            {
                return View();
            }

            return View(viewModel);   
        }

        [HttpPost]
        public ActionResult SearchTravel(SearchTravelViewModel data)
        {
            if (data != null)
            {
                return RedirectToAction("ListTravel",
                    new
                    {
                        serviceID = data.serviceID,
                        isAdvance = data.isAdvance.ToString(),
                        loaiTour = data.loaiTour,
                        ngayDi = data.ngayDi.ToShortDateString(),
                        ngayVe = data.ngayVe.ToShortDateString(),
                        giaMin = data.giaMin.ToString(),
                        giaMax = data.giaMax.ToString(),
                        diaDiem = data.diaDiem,
                        diemDi = data.diemDi,
                        diemDen = data.diemDen,
                        soNguoiLon = data.soNguoiLon.ToString(),
                        soTreEm = data.soTreEm.ToString(),
                    });
            }

            return RedirectToAction("SearchTravel", new { serviceID = data.serviceID});
        }

        public ActionResult ListTravel(
            string serviceID,
            string isAdvance,
            string loaiTour,
            string ngayDi,
            string ngayVe,
            string giaMin,
            string giaMax,
            string diaDiem,
            string diemDi,
            string diemDen,
            string soNguoiLon,
            string soTreEm)
        {
            SearchTravelViewModel data = new SearchTravelViewModel();
            data.serviceID = serviceID;
            data.isAdvance = bool.Parse(isAdvance);
            data.loaiTour = loaiTour;
            data.ngayDi = DateTime.Parse(ngayDi);
            data.ngayVe = DateTime.Parse(ngayVe);
            data.giaMin = double.Parse(giaMin);
            data.giaMin = double.Parse(giaMax);
            data.diaDiem = diaDiem;
            data.diemDi = diemDi;
            data.diemDen = diemDen;
            data.soNguoiLon = int.Parse(soNguoiLon);
            data.soTreEm = int.Parse(soTreEm);

            bool isError = false;
            var service = (TravelSerivce)ServiceFactory.GetService(serviceID);
            ListTravelViewModel viewModel = new ListTravelViewModel();

            List<TourDuLich> listTour = null;
            try
            {
                if (data.isAdvance)
                {
                    listTour = service.SearchForTour(
                            data.loaiTour,
                            data.ngayDi,
                            data.ngayVe,
                            data.giaMin,
                            data.giaMax,
                            data.diaDiem);
                }
                else
                {
                    listTour = service.RetrieveTourList(data.loaiTour, data.diemDi);
                }                
            }
            catch (Exception ex)
            {
                isError = true;
            }

            if (isError || serviceID == null || listTour == null)
            {
                return View();
            }

            viewModel.serviceID = serviceID;
            viewModel.dsTour = listTour;
            viewModel.soNguoiLon = data.soNguoiLon.ToString();
            viewModel.soTreEm = data.soTreEm.ToString();

            return View(viewModel);
        }

        public ActionResult DetailTravel(string serviceID, string tourID, string adult, string children)
        {
            bool isError = false;
            var service = (TravelSerivce)ServiceFactory.GetService(serviceID);
            DetailTravelViewModel viewModel = new DetailTravelViewModel();

            TourDuLichChiTiet tour = null;
            try
            {
                tour = service.RetrieveTourDetail(tourID);
            }
            catch (Exception ex)
            {
                isError = true;
            }

            if (isError || serviceID == null || tour == null)
            {
                return View();
            }

            viewModel.serviceID = serviceID;
            viewModel.tourID = tourID;
            viewModel.tourDetail = tour;
            viewModel.adult = adult;
            viewModel.children = children;

            return View(viewModel);
        }

        [Authorize]
        public ActionResult BookTravel(string serviceID, string tourID, string adult, string children)
        {
            bool isError = false;
            var service = (TravelSerivce)ServiceFactory.GetService(serviceID);
            BookTravelViewModel viewModel = new BookTravelViewModel();

            TourDuLichChiTiet tour = null;
            try
            {
                tour = service.RetrieveTourDetail(tourID);
            }
            catch (Exception ex)
            {
                isError = true;
            }

            if (isError || serviceID == null || tour == null)
            {
                return View();
            }

            viewModel.serviceID = serviceID;
            viewModel.tourID = tourID;
            viewModel.tourDetail = tour;
            viewModel.adult = adult;
            viewModel.children = children;
            viewModel.listPassenger = new List<KhachHang>();
            for (int i = 0; i < viewModel.numberPassenger; i++)
                viewModel.listPassenger.Add(new KhachHang());

            return View(viewModel);
        }

        [Authorize]
        [HttpPost]
        public ActionResult BookTravel(BookTravelViewModel data)
        {
            if (data == null)
                return RedirectToAction("BookTravel", 
                    new { serviceID = data.serviceID, tourID = data.tourID, adult = data.adult, children = data.children });

            int? maHoaDon = null;
            bool isError = false;
            
            var service = (TravelSerivce)ServiceFactory.GetService(data.serviceID);
            try
            {
                maHoaDon = service.BookingForTour(int.Parse(data.tourID), int.Parse(data.adult), 
                    int.Parse(data.children), data.listPassenger);
            }
            catch (Exception ex)
            {
                isError = true;
            }

            if (isError || maHoaDon == null)
                return RedirectToAction("BookTravel",
                    new { serviceID = data.serviceID, tourID = data.tourID, adult = data.adult, children = data.children });

            string orderDetail =
                @"Hãng du lịch: " + DatabaseHelper.GetServiceName(data.serviceID) +
                @"\nTên tour: " + data.tourDetail.tenTour +
                @"\nSố ngày: " + data.tourDetail.soNgay.ToString();

            DatabaseHelper.AddOrderDetail(
                data.serviceID,
                DatabaseHelper.GetUserID(User.Identity.Name),
                orderDetail,
                (int)data.tourDetail.giaTour);

            return View();
        }
        
        //public ActionResult TourBooking()
        //{
        //    TourBookingViewModel model = (TourBookingViewModel)TempData["TourBookingModel"];
        //    if (model.dsHanhKhach == null || model.dsHanhKhach.Count == 0)
        //    {
        //        model.dsHanhKhach = new List<KhachHang>();
        //    }
        //    var service = ServiceFactory.Travel.FirstOrDefault(m => m.ServiceID.Equals(model.serviceID));
        //    TourDuLichChiTiet result = service.RetrieveTourDetail(model.tourID);
        //    if (result != null)
        //    {
        //        model.tourInfo = result;
        //        return View(model);
        //    }

        //    TempData["TravelServiceError"] = "Xảy ra lỗi trong quá trình lấy dữ liệu ở nhà cung cấp";
        //    return RedirectToAction("SearchTravel");
        //}

        //[HttpPost]
        //public ActionResult TourBooking(TourBookingViewModel model)
        //{
        //    return View();
        //}
    }
}
